<template>
	<view>
		<view class="p-3">
			<view v-if="dataList.length > 0">
				<view class="bt-block mb-3" v-for="item, idx in dataList" :key="idx" @click="gotoCheck(item)">
					<bg-cell :border="false">
						<view class="flex justify-between align-center">
							<view>
								<bg-desc :label="item.projectName" :labelStyle="{ width: '240rpx' }"
									:value="item.workJob"></bg-desc>
								<bg-desc label="入场时间" :labelStyle="{ width: '240rpx' }"
									:value="item.entryTime"></bg-desc>
							</view>
							<view>
								<bg-btn style="font-size24rpx;height:52rpx; width:130rpx; padding: 0;">
									<view class="font-11">申请退场</view>
								</bg-btn>
							</view>
						</view>
					</bg-cell>
				</view>
			</view>
			<u-empty v-else></u-empty>
			<bg-confirm :content="`请确认是否退出《${this.selectProject.projectName}》， 一经退出不可恢复`" @confirm="confirmModal"
				@close="closeModal" :show.sync="show">
			</bg-confirm>
			<bg-confirm :popupTitle="'提示'" :content="`为保证项目至少有一名班组长，您暂时不可以退场该项目`" @confirm="showNotWork = false"
				@close="showNotWork = false" :show.sync="showNotWork">
			</bg-confirm>
		</view>
		<u-popup :closeable="true" :show="showTeamPopup" :round="10" mode="center" @close="showTeamPopup = false"
			@open="showTeamPopup = true">
			<view style="width: 700rpx; padding: 10rpx;">
				<view class="text-center font-15 font-bold mb-4">退场前请先选择要交接的班组长</view>
				<view class="my-4 font-15 font-bold">
					交接班组长
				</view>
				<view class="">
					<uni-data-select v-model="value" :localdata="range" @change="change"></uni-data-select>
				</view>
				<view class="bt-gap flex justify-around" style="margin-top: 60rpx;">
					<bg-btn @click="showTeamPopup = false"
						style="height:90rpx; color: #86898c;background-color: #fafafa; border: none;">取消</bg-btn>
					<bg-btn @click="confirmTeam" style="height:90rpx">确定</bg-btn>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		retireProject,
		exitSign,
		isTeamBind,
		queryTeamList,
		teamUnbind,
		canExit
	} from '../../../api/retire'
	export default {
		data() {
			return {
				show: false,
				dataList: [],
				selectProject: {},
				value: 0,
				range: [],
				showTeamPopup: false,
				showNotWork: false
			}
		},
		mounted() {
			this.getData()
			uni.$on("refresh-page", () => {
				this.getData()
			})
		},
		methods: {
			getData() {
				retireProject().then(res => {
					this.dataList = res.data
				})
			},
			closeModal() {
				this.show = false;
			},
			confirmModal() {
				let p = this.selectProject
				let obj = {
					projectId: p.projectId,
					userId: this.$store.getters.userId
				}
				if (this.$auth.hasRoleOr(["team"])) {
					obj.newLeaderId = this.value
				}
				exitSign(obj).then((res) => {
					this.show = false
					this.value = 0
					this.getData()
					uni.navigateTo({
						url: "/pages/common/webview/index?url=" + encodeURIComponent(res.data.signUrl) +
							"&title=合同签约"
					})
				})
			},
			async gotoCheck(p) {
				if (this.$auth.hasRoleOr(["team"])) {
					this.selectProject = p;
					this.getQueryTeamList();
					// const isBind = await this.getIsTeamBind();
					// if (isBind) {
					// this.getQueryTeamList();
					// this.showTeamPopup = true
					// } else {
					// 如果没绑定可以执行其他逻辑，比如提示、跳转绑定页等
					// this.$toast("请先绑定工人");
					// this.showNotWork = true
					// }
					return
				}
				this.selectProject = p;
				this.show = true
			},
			async getIsTeamBind() {
				let p = this.selectProject;
				try {
					let res = await isTeamBind({
						projectId: p.projectId,
						userId: this.$store.getters.userId
					});
					// 根据返回值判断是否绑定工人，大于0 绑定了工人
					return res.data > 0; // 返回 true 表示已绑定
				} catch (error) {
					console.error("getIsTeamBind error", error);
					return false;
				}
			},
			async getQueryTeamList() {
				let p = this.selectProject
				let res = await queryTeamList(p.projectId)
				if (res.code == 200) {
					if (res.data.length > 0) {
						this.range = res.data.map((item) => {
							item.text = item.realName
							item.value = item.userId
							return item
						})
						console.log('this.range', this.range);
						setTimeout(() => {
							this.showTeamPopup = true
						}, 1000)
					} else {
						// this.$modal.msg("请先绑定工人")
						// this.showNotWork = true //不走提示弹窗

						let response = await canExit()
						if (response.code == 200 && response.data) {
							this.confirmModal()
						} else {
							this.showNotWork = true
						}
					}
				} else {
					this.$modal.msg(res.msg)
				}


			},
			async confirmTeam() {
				if (this.value <= 0 || !this.value) return this.$modal.msg("请选择交接人员")
				let p = this.selectProject
				let res = await teamUnbind({
					projectId: p.projectId,
					userId: this.$store.getters.userId,
					newUserId: this.value
				})
				if (res.code == 200) {
					this.$modal.msg("交接成功")
					this.showTeamPopup = false
					this.show = true
				} else {
					this.$modal.msg(res.msg)
				}
			}
		}
	}
</script>

<style></style>